using Publish.Utility;
using System.Threading;

namespace Publish.Services
{
    public class Service
    {
        /// <summary>
        /// 简单数据库操作类
        /// </summary>
        public static readonly SqlHelper sqlHelper = new SqlHelper();

        #region Method

        #region Public Method

        /// <summary>
        /// 验证表是否存在
        /// </summary>
        public static void CheckTableThreadStart()
        {
            var checkTableThread = new Thread(CheckTable) { IsBackground = true };
            checkTableThread.Start();//验证表是否存在
        }

        #endregion

        #region Private Method

        /// <summary>
        /// 验证表是否存在
        /// </summary>
        private static void CheckTable()
        {
            #region Create Table Sql

            //发布版本信息
            var publishTableCreate = @"IF(NOT Exists(SELECT Name FROM SYS.objects WHERE name = N'Publish'))

                                        CREATE TABLE [Publish](
	                                        [Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
	                                        [FolderName] [nvarchar](255) NOT NULL,
	                                        [Type] [int] NOT NULL,
	                                        [Files] [nvarchar](4000) NOT NULL,
	                                        [CreatorIPv4] [nvarchar](255) NOT NULL,
	                                        [CreatorHostName] [nvarchar](255) NOT NULL,
	                                        [CreatedTime] [datetime] NOT NULL
                                        )";

            //发布文件版本信息
            var publishFileVersionTableCreate = @"IF(NOT Exists(SELECT Name FROM SYS.objects WHERE name = N'PublishFileVersion'))

                                    CREATE TABLE [PublishFileVersion](
	                                    [Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
	                                    [ProjectName] [nvarchar](255) NOT NULL,
	                                    [LastWriteTime] [datetime] NOT NULL,
	                                    [Size] [decimal](19, 5) NOT NULL,
	                                    [Path] [nchar](500) NOT NULL,
	                                    [CreatorIPv4] [nvarchar](255) NOT NULL,
	                                    [CreatorHostName] [nvarchar](255) NOT NULL,
	                                    [CreatedTime] [datetime] NOT NULL,
	                                    [ModifierIPv4] [nvarchar](255) NOT NULL,
	                                    [ModifierHostName] [nvarchar](255) NOT NULL,
	                                    [ModifiedTime] [datetime] NOT NULL,
                                    )";

            #endregion

            sqlHelper.ExecuteNonQuery(publishTableCreate);

            sqlHelper.ExecuteNonQuery(publishFileVersionTableCreate);
        }

        #endregion

        #endregion
    }
}
